package org.hot100_6.leetcode49;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class Solution3 {
    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String,List<String>> resMap = new HashMap<>();
        for (String str : strs) {
            char[] chars = str.toCharArray();
            Arrays.sort(chars);

            String item = new String(chars);
            List<String> list = resMap.getOrDefault(item, new ArrayList<>());
            list.add(str);
            resMap.put(item,list);
        }

        List<List<String>> res = new ArrayList<>(resMap.values());
        return res;
    }
}
